import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import Home from '../views/Home.vue';

const routes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/',
    component: () => import('../components/layout/BasicLayout.vue'),
    children: [
      {
        path: '',
        name: 'home',
        component: Home
      },
      {
        path: 'ai-efficiency',
        name: 'ai-efficiency',
        component: () => import('../views/AIEfficiency/AIEfficiency.vue'),
        redirect: '/ai-efficiency/apps',
        meta: { breadcrumb: 'AI提效' },
        children: [
          {
            path: 'models',
            name: 'ai-models',
            component: () => import('../views/AIEfficiency/AIEfficiencyModels.vue'),
            meta: { breadcrumb: 'AI模型' }
          },
          {
            path: 'apps',
            name: 'ai-apps',
            component: () => import('../views/AIEfficiency/AIEfficiencyApps.vue'),
            meta: { breadcrumb: 'AI应用' }
          },
          {
            path: 'knowledge',
            name: 'knowledge-base',
            component: () => import('../views/AIEfficiency/AIEfficiencyKnowledge.vue'),
            meta: { breadcrumb: '知识库' }
          },
          {
            path: 'mcp',
            name: 'ai-mcp',
            component: () => import('../views/AIEfficiency/AIEfficiencyMCP.vue'),
            meta: { breadcrumb: 'MCP' }
          }
        ]
      },
      {
        path: 'test-tools',
        name: 'test-tools',
        component: () => import('../views/TestTool/TestTool.vue'),
        redirect: '/test-tools/platform',
        meta: { breadcrumb: '测试工具' },
        children: [
          {
            path: 'platform',
            name: 'test-platform',
            component: () => import('../views/TestTool/TestToolPlatform.vue'),
            meta: { breadcrumb: '测试平台' }
          },
          {
            path: 'statistics',
            name: 'test-statistics',
            component: () => import('../views/TestTool/TestToolStatistics.vue'),
            meta: { breadcrumb: '度量看板' }
          },
          {
            path: 'settings',
            name: 'test-settings',
            component: () => import('../views/TestTool/TestToolSettings.vue'),
            meta: { breadcrumb: '设置' }
          },
      
        ]
      },
      {
        path: 'system',
        name: 'system',
        component: () => import('../views/Settings/Settings.vue'),
        redirect: '/system/toolManagement',
        meta: { breadcrumb: '系统配置' },
        children: [
          {
            path: 'toolManagement',
            name: 'toolManagement',
            component: () => import('../views/Settings/ToolManagement.vue'),
            meta: { breadcrumb: '工具管理' }
          },
          {
            path: 'basicSettings',
            name: 'basicSettings',
            component: () => import('../views/Settings/BasicSettings.vue'),
            meta: { breadcrumb: '基本配置' },
            children: [
              {
                path: 'userManagement',
                name: 'userManagement',
                component: () => import('../views/UserManagement/UserList.vue'),
                meta: { breadcrumb: '用户管理' }
              },
              {
                path: 'featureManagement',
                name: 'featureManagement',
                component: () => import('../views/FeatureManagement/FeatureList.vue'),
                meta: { breadcrumb: '特性管理' }
              }
              
            ]
          },
        
        ]
      },
      {
        path: '/documents/:id/',
        name: 'ai-documents',
        component: () => import('../views/AIEfficiency/KnowledgeDocuments/KnowledgeDetail.vue'),
        props: true
      },
      {
        path: '/codder/:id/',
        name: 'ai-codder',
        component: () => import('../views/AIEfficiency/KnowledgeCodder/KnowledgeDetail.vue'),
        props: true
      },
      {
        path: '/relation/:id/',
        name: 'ai-relation',
        component: () => import('../views/AIEfficiency/KnowledgeRelation/KnowledgeDetail.vue'),
        props: true
      }
    ]
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 模拟判断用户是否登录的函数
function isAuthenticated() {
  // 检查本地存储中的 token 和 userInfo
  return localStorage.getItem('token')!== null && localStorage.getItem('userInfo')!== null;
}

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
  // 如果目标路由不是登录页且未通过认证
  if (to.name!== 'login' &&!isAuthenticated()) {
    next({ name: 'login' }); // 跳转到登录页
  } else {
    next(); // 正常放行
  }
});

export default router;
